Hi!

I learnt the C language (NOT C++) but the concept of threading is completely new to me. I'm having trouble finding a good tutorial or book on the subject.
Can some one point me to a tutorial or book about threading specifically using C?

I use Visual C++ 2005 (7.0) someitmes 6.0 as my compiler.

Thanks

I wrote this example as a reply to one earlier post about interprocess communication (sockets there provide two-way communication between threads):

#include <stdio.h>
#include <sys/socket.h>

int sv [2];

int main ()
{
  socketpair (AF_UNIX,
    SOCK_STREAM, 0, sv);
  if (fork ()) { /* parent */
    char buf [MAX_INPUT]; 
    char sbuf [MAX_INPUT];
    FILE *f;

    f = fopen ("prodfile", "w");
    while (1) {
      if (!fgets (buf, MAX_INPUT, stdin))
        break;
      fputs (buf, f);
      fflush (f);
      send (sv [0], "read",
        MAX_INPUT, 0);
      while (recv (sv [0], sbuf, 
        MAX_INPUT, MSG_DONTWAIT) == -1)
        usleep (10000);
      if (strcmp (sbuf, "send")) break;
    }
  } else { /* child */
    char buf [MAX_INPUT];
    char sbuf [MAX_INPUT];
    FILE *f;

    f = fopen ("prodfile", "r");
    while (1) {
      while (recv (sv [1], sbuf, 
        MAX_INPUT, MSG_DONTWAIT) == -1)
        usleep (10000);
      if (strcmp (sbuf, "read")) break;
      if (!fgets (buf, MAX_INPUT, f))
        break;
      fputs (buf, stdout);
      send (sv [1], "send",
        MAX_INPUT, 0);
    }
  }
  return 0;
}

In essence, after the function fork () an exact copy of the program would be created, and two identical copies of the code would run starting from the function fork (). Which means that fork () returns into both threads, with the only difference that in one thread it returns 0, and in the other thread it returns 1, which is the only way how the code finds in what thread it runs.

Unfortunately i don't know, how the threading is done in visual c, but if it is standard compliant, then it should implement fork (), which is the way to start threads by posix standard. The only open source compiler which implements fork in windows, is cygwin, mingw doesn't implement fork, but can implement threads using windows api functions. Unfortunately i don't know the windows api functions to start threads, i know only the cross-platform functions, but maybe someone here can explain these.

BTW this, running several identical copies of the code, is a general method for multiprocessing. For threads, a new copy of the code would be made inside code, but in supercomputers often hundrieds of identical programs run in parallel, each in separate processor, also often processing an identical data, except that in the time-critical moments each do a separate part of processing, communicating the results if necessary to the other processes by fast ethernet or shared memory. This is the way of processing which many programmers likely would have to know, as computers with tens or hundrieds of processors would once likely be common.

Now the other thread of me was spawned, which says that fork () does not exactly create a copy of the code, but creates a new process which runs the same code in the memory. I tried to explain it in a simplified way. The difference it makes, is that the global and shared variables would be common for threads, because the code in memory which they run, is one and the same. Running the same code simultaneously by several processes may also happen at running some kernel functions. The way to avoid the problems which that may cause, is making the code so-called "re-entrant", which in essence means using global and shared variables as few as possible, avoiding the conflicts from using the existing global variables, and implementing freeing the memory of commonly used objects by dereferencing, freeing it only when no thread would no longer need the object.

I learnt the C language (NOT C++) but the concept of threading is completely new to me. I'm having trouble finding a good tutorial or book on the subject.
Can some one point me to a tutorial or book about threading specifically using C?

I use Visual C++ 2005 (7.0) someitmes 6.0 as my compiler.

C does not have threads, but C libraries do. Perhaps look into pthreads? A tutorial for it is listed in the External Links. And BTW, avoid MSVC6.

Well in the windows api there is a function CreateThread which is similar to pthreads function pthread_create, this is the msdn link http://msdn2.microsoft.com/en-us/library/ms682516.aspx Visual c certainly implements at least this function. In essence, these functions have a function pointer as an argument, which is the function where the new thread would start to run, this is a bit different from fork () which is posix as well, somewhat simpler, and shows somewhat better that which happens when the new thread would be created.

When writing MS-Windows programs I always used CreateThread() because it seemd to be simpler. There are several pitfalls with threads -- specifically you have to snychronize the use of variable and code that are commen to all threads. For example suppose you have a global variable that is set by one thread and read or set by a second thread. You have to use something that allows only one thread at a time to access that variable, normally by use of semiphore or critical threads. Also make sure the functions that each thread calls are thread-safe, normally the function's documentation will indicate whether it is or not. If not, then here again you will have to synchronize access to the functions similar to how I described above. The tutorial Dave posted might help a little, but Microsoft compilers don't support the POSIX methods described in that link.

Hi

If u r working under linux the Pthread library supports functions to create threads and work on them.

#include<pthread.h>
#include <stdio.h>
#include <stdlib.h>

void foo_Thread(char *input) {
 printf("Inside  %s", input);
  }

int main(void) {

pthread_t thread1, thread2;
     char *str1 = "Thread one";
     char *str2 = "Thread two";
     int  ret1, ret2;

    /* Create independent threads each of which will execute function */

     ret1 = pthread_create( &thread1, NULL, foo_Thread, (void*) str1);
     ret2 = pthread_create( &thread2, NULL, foo_Thread, (void*) str2);

     /* Wait till threads are complete 
     pthread_join( thread1, NULL);
     pthread_join( thread2, NULL); 

     printf("Thread one returns: %d\n",iret1);
     printf("Thread two returns: %d\n",iret2);
     exit(0);

}

hello all, How can I create thread in MS C 6.0. Plz tell me.

> hello all, How can I create thread in MS C 6.0. Plz tell me.
Did you bother to read any of the posts in this thread AT ALL?

Hi

If u r working under linux

He isn't.

Actually the system I will be working with is POSIX based...
But since I'm new to Multi-Threading and I currently use VS 7 on Windows I figured I would learn it thru Windows.

I found a good tutorial on CreatThread(). I'm think I'm finally starting to get the picture.
I still have some unresolved issues.
For one, Where does using a "callback" function come in when dealing with threads?

>>Actually the system I will be working with is POSIX based... But since I'm new to Multi-Threading and I currently use VS 7 on Windows I figured I would learn it thru Windows.

I think the only difference between POSIX and MS-Windows is the way threads are created. The actual thread functions should be pretty much the same except of course for os-specific function calls within the thread function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.