-1

This is my first contact with c and Multithreading.

I look for an example how to use it in Basic.

This is what I want to to parallel:

PseudoCode that should run on different Threads

Code1


int main(){

   int i =1000;

   while(i>0){
   i--;
   }
}

Code2


int main(){

   int x =0;

   if(i%5){
   x++;
   }
}

I also didn't know how to pass Objects, maybe someone can explaine this too.

Community
  • 1
  • 1
Alpha-Centauri
  • 316
  • 2
  • 13

1 Answers1

1

Please use only lowercase in C to declaring variables and functions as struct,typeof,sizeof,int,char,void,for,while,etc....

EDIT: Sorry for not to understand "I thought you need help to do that code in C"

Now i know that you want multi-threading functions to do two jobs or more at the same time without waiting for other to finish.

Okay, to do that you have to

  • include pthread.h which means POSIX Thread

Note: you should notice from this name that this library for Linux OS only

and you compile it with gcc compiler

  • declare variable pthread to for example: tid in your main() as the most popular name
  • Create your function in void *() and type stuff whatever it does then create your thread in main() and assign it your function through that code:

    pthread_create(&tid, NULL, MyThread, NULL);

pthread_create() arguments:

  1. A pointer to a pthread_t structure that we created to fill it with the upcoming arguments.

  2. A pointer to a pthread_attr_t with parameters for the thread. You can safely just pass NULL most of the time.

Note: The pthread_attr_t > "arg 2" should be treated as opaque: any access to the object other than via pthreads functions is nonportable and produces undefined results.

  1. Take the function that you created as thread and shall be with no return and
    points to >> void, that's why we created void *()

Note: Type only the name of function without (), you will know why in the next argument

  1. Here you passing your arguments to your function!, if there's no arguments just pass NULL

Example Code:

#include <stdio.h>
#include <pthread.h>
int i=0,x=0; //Initialize our variables
void *MyThread(void *ANYarg)  //arguments must be a pointers to point from `pthread_create` with `NULL` if no need
{
    while(1) //background thread
        {
            i++;
            x++;
        }
    return NULL;
}

int main()
{
    char *input;
    pthread_t tid; //Declare a thread
    pthread_create(&tid, NULL, MyThread, NULL); //Create the thread
    while(1) //Printing thread , Uncover increamting of variables
        {
            scanf("%s",&input); //whenever you input a value
            printf("i: %d, x: %d\n",i,x);//will print the i,x values now
        }
    return 0;
}

Note: You should at least create one pointer variable specifically to your thread function cause fourth argument of pthread_create() need at least one to pass the NULL value

You can wait for an thread to finish instead of doing work at the same time through that code line:

pthread_join(tid, NULL);

Should return 0 when success

An Example:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *MyCoolthread(void *vargp)
{
    printf("Yes..see me printing success after 3 seconds \n");
    sleep(3);
    printf("Success! \n");
    return NULL;
}

int main()
{
    pthread_t tid;
    printf("Hello, are you there? \n");
    sleep(1);
    pthread_create(&tid, NULL, MyCoolthread, NULL);
    pthread_join(tid, NULL);    
    printf("Yup i see!\n");
    return 0;
}

Try to comment pthread_join(tid, NULL); with // to see what happens

and to terminate your thread is through code line:

pthread_exit(&tid);

Doesn't return to its caller any value

Edit: I noticed now that you use windows so i advice you to dual boot with Ubuntu instead if you really interested in C

otherwise you can use multi-threading in windows.h header that called winapi library but i'm not expert in so you can find an simple example here and you should mentioned in your post that you want for windows by the way you can edit to improve it

I tried my best to get it clear, hope it helps.

Beyondo
  • 2,952
  • 1
  • 17
  • 42
  • Note: i see no need for using `i` or even increment it but it will be useful if you put it as an condition : `while(i<10){i++;stuff..}` to `limit` the number of inputs and exit `while-loop` – Beyondo Sep 01 '17 at 16:30
  • Okay, maybe my question was not asked that good. But this is not what I'm looking for. I would like to use multi threading, so I can do two things at the same time. – Alpha-Centauri Sep 02 '17 at 05:46
  • I didn't know that you mean multi-threading by the way I completely edited the answer ^^ – Beyondo Sep 02 '17 at 16:35
  • Thx, this is very nice! :) – Alpha-Centauri Sep 02 '17 at 20:41