LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Can we actually create threads in C? (https://www.linuxquestions.org/questions/programming-9/can-we-actually-create-threads-in-c-682798/)

aarontwc 11-12-2008 02:21 AM

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!!

Nylex 11-12-2008 02:34 AM

What about pthreads?

abolishtheun 11-12-2008 02:35 AM

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".

aarontwc 11-12-2008 05:47 AM

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!!

pinniped 11-12-2008 06:11 AM

Quote:

Originally Posted by aarontwc (Post 3339316)
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.

nehaandrew 11-26-2008 11:20 AM

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

dwhitney67 11-27-2008 07:56 AM

Quote:

Originally Posted by nehaandrew (Post 3355520)
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().


All times are GMT -5. The time now is 04:45 AM.