LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-12-2008, 02:21 AM   #1
aarontwc
Member
 
Registered: Oct 2008
Posts: 66

Rep: Reputation: 15
Can we actually create threads in C?


I have an existing program written in C. But for some reason I would like to add threading to it. I readup some guides online, it was all implementation using C++.

I tried in my C program to just very simply include the thread.h header. But it does not find it in the standard library.

I am assuming that I can't actually program using threads in C conventionally. Is there any way to get around this? Or am i just missing something really simple?

Converting my C program to C++ is the last thing I want to do. So unless I don't have an alternative, I doubt I want to go by this route

Thanks!!
 
Old 11-12-2008, 02:34 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
What about pthreads?
 
Old 11-12-2008, 02:35 AM   #3
abolishtheun
Member
 
Registered: Mar 2008
Posts: 183

Rep: Reputation: 31
of course you can create threads in C. Whatever C++ threading lib you were looking at, it was calling the underlying threading functions provided by the OS, and those are written in C (at least on *nix). Google "POSIX threads" or "pthreads".

But just because you can use threads from C doesn't mean you should. There are safer, easier ways of doing multiple things at once. Unix (and linux in particular) provides cheap processes and a plethora of IPC options (from sockets to pipes to shared memory). And unless you're doing something that actually requires the performance gain yielded by threads like heavy number crunching, using processes is fast enough. In general, threads are not the answer, they are the question. And the answer is almost always "No".
 
Old 11-12-2008, 05:47 AM   #4
aarontwc
Member
 
Registered: Oct 2008
Posts: 66

Original Poster
Rep: Reputation: 15
Thanks for your comments! I actually tried out fork. and the program works fine. Will try out pthread when i have a chance. Thank you!!
 
Old 11-12-2008, 06:11 AM   #5
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
Quote:
Originally Posted by aarontwc View Post
Thanks for your comments! I actually tried out fork. and the program works fine. Will try out pthread when i have a chance. Thank you!!
fork() creates a different process so no memory (and therefore no variables) are shared between the parent and child, although variables will have the same value immediately after the cloning of the process - except of course for the return value of fork().

Using 'pthreads' the different 'threads' share memory.

Things are actually a bit more complicated - you'll have to read through the man pages for fork() and pthreads to see what's shared and what's not.

Personally I use pthreads more often than fork() because it saves me the effort of setting up shared memory blocks or pipes. UNIX survived without 'threads' for a long time (fork() was the only option) so in most cases the choice is a matter of personal preference. fork() and pthreads are very different beasts though and you pretty much have to decide from the start which one you'll use because that will have a huge impact on how you design the code. Switching between the two is not trivial (unless your program is very trivial to begin with). pthreads *do* make some tasks much easier though - for one, you don't need to resize the shared memory all the time and somehow signal the other processes that the size has changed etc etc.
 
Old 11-26-2008, 11:20 AM   #6
nehaandrew
Member
 
Registered: Nov 2008
Posts: 53

Rep: Reputation: 15
Coding Threads in C and C++ is such a tiresome and extensive process. This is where Java kicks ass. Creating threads is as simple as :

Thread tt = new Thread("My Thread - 1");
tt.start();



Long live James Gosling


Linux

Last edited by nehaandrew; 11-30-2008 at 01:19 AM.
 
Old 11-27-2008, 07:56 AM   #7
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by nehaandrew View Post
Coding Threads in C and C++ is such a tiresome and extensive process. This is where Java kicks ass. Creating threads is as simple as :

Thread tt = new Thread("My Thread - 1");
tt.start();



Long live James Gosling
Um, in C++ it is:
PHP Code:
boost::threadtt = new boost::thread(MyThread());
tt->join(); 
where MyThread is a functor object that defines/implements operator().
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to create Timer based threads mugdha Programming 5 04-18-2008 08:16 AM
Execution threads vs normal threads jonty_11 Linux - General 2 03-26-2008 10:37 AM
Using system calls to create/context switch threads in assembly code casmac Programming 1 07-13-2007 04:48 PM
How to create more than 2000 threads inlinux 2.4kernel Agnello Programming 5 07-11-2006 09:12 AM
Java threads listed using kill -3 does not contain all threads found using ps -auxww coneheed Programming 2 11-14-2005 08:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration