Cesar D. Rodas, web development. Technology news. PHP, MySQL, Apache, C, Bash, ASM

C and Threads

Java is very loved in the real world because you only write one time and compile and it should run on m$ and Unix like OS and others.

I was trying to do something with Java. A simple server soap client. Basically read a get from MySQL information and connect to many SOAP servers. I was trying to but I couldn’t, I had many problems in the thread creating with. Then I try to do this but in C, and it work.

Here I put a portion of my code for manage Thread, basically what it does is to create and destroy threads. And It was tested on m$, CentOS 4.x, CenOS 3.x, RH 9, and SuSE and works good.

The source is public domain.

saddor.thread.c

#include “saddor.threads.h”

uint NumberOfThreads; /* number of threads */

bool ThreadBegin(ThreadType * thread, void (*callback)(void *), void *parameter) {
#ifdef WIN32
*thread = _beginthread((void *)callback, 0, parameter);
if (thread == -1)
return false;
#else
if ( pthread_create(thread, NULL, callback, parameter) )
return false;
#endif
NumberOfThreads++;
return true;
}

saddor.threads.c

#ifndef STHREAD_H
#define STHREAD_H

#ifdef WIN32
#include <windows.h>
#define ThreadType int
#define ThreadEnd if (NumberOfThreads > 0) {NumberOfThreads–;} _endthread();
#else
#include <pthread.h>
#define closesocket(x) close(x)
#define ThreadType pthread_t
#define ThreadEnd if (NumberOfThreads > 0) NumberOfThreads–; pthread_exit(0);

#endif

#endif

example.c

#include <stdio.h>
#include <stdlib.h>
#include “saddor.thread.h”

void * threadtest(void *param)
{
int *i,e;
i= (int *)param;
for (e=0; e<100; e++) {
printf(”Thread %d: %d\n”,i,e);
}
ThreadEnd; // killing the thread

}
#ifndef WIN32

void Sleep(int mseconds) {
struct timespec timeOut,remains;
timeOut.tv_sec = 0;
timeOut.tv_nsec = (long)mseconds * 10000000; /* 50 milliseconds */
nanosleep(&timeOut, &remains);
}
#endif

int main(int argc, char *argv[])
{
int i;
ThreadType thread;
for(i=0; i < 1000; i++)
{
ThreadBegin(&thread, threadtest, (void*)i);
}
/* while the thread exist.. do something*/
while (NumberOfThreads > 0)
{
printf(”Number of threads %d\n\n”,NumberOfThreads);
Sleep(30); /* sleep 30 miliseconds*/
}

return 0;
}

I recomment to take a look here ;)

Have a lot of fun with C ;)

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages. Digg del.icio.us StumbleUpon Technorati BlinkList Furl NewsVine Reddit

2 Responses to “C and Threads”

  1. LuisC Says:

    Good beginning, but there is more to do with threads than only Create and Delete it. I think it can grow to be an “portable way” to programming threads in C to Win and *NIX OSes, using their “native” thread library.
    Don’t you have searched on the net to see if there are another similar projects anywhere? Maybe in sf.net?

    Great post! Remember me when you’re hired to work in Google :P

  2. Cesar D. Rodas Says:

    Basically for me is only required create and destroy thread at this time. When I need more things I will find another way of portability using C.
    And of course if i work on Google.. you will go with me ;)

Leave a Reply


Fatal error: Call to undefined function display_cryptographp() in /home/.schmuckie/saddor/cesarodas.com/wp-content/themes/first-spring-10/comments.php on line 94