I am trying to make a simple HTTP/HTTPS service discovery in C.

For this app i use pthread as there is no other better cross platform options for this, but i get some strange errors.

PROBLEM1 : thread_submit has do be dynamically modified at the run time. Right now it is only accepting lvalue and it can only be modified from source code only.

int thread_max = 50; // how many checking threads to spawn [suggesting number between 4 and 8]
#define thread_submit 50 // how many thread submits, usually has to be lower or equal to thread_max

typedef struct Task {
    int id;
    char* ip;
    int port;
} Task;

Task taskQueue[thread_submit];
int taskCount = 0;

After a while (first for iteration) the program stops.

PROBLEM2 : after first iteration, program waits

int main()
{
    for(int this_port = 80; this_port <= 88; this_port++)
    {
        ...

        pthread_t thread_scan;
        pthread_t thread_read;
        pthread_create(&thread_scan, &attr, &run_fake_scan, NULL);
        pthread_create(&thread_read, &attr, &read_results, NULL);
        pthread_join(thread_scan, NULL);
        pthread_join(thread_read, NULL);
        pthread_attr_destroy(&attr);

        ...
    }
}

Output:

read_results -> Thread created no. 44
read_results -> Thread created no. 45
startThread -> i am here 1
read_results -> Thread created no. 46
startThread -> i am here 1
startThread -> i am here 1
read_results -> Thread created no. 47
read_results -> Thread created no. 48
read_results -> Thread created no. 49
startThread -> i am here 1
startThread -> i am here 1

It is stopping/waiting on pthread_mutex_lock(&mutexQueue); line, as if is infinitely waiting for mutex to get unlocked and never does that.

Full source code : https://pastebin.com/a6rtTBcX
Run code : https://www.onlinegdb.com/edit/EAou_Yzol

What am i doing wrong?

Thank you.

It looks like you're encountering a deadlock in your code, where two threads are waiting for each other to release a mutex before they can continue. This can happen if a thread tries to acquire a mutex that it already holds, or if two threads try to acquire the same mutex at the same time without using a proper synchronization mechanism.

In your code, it looks like run_fake_scan is trying to acquire the mutexQueue mutex, but read_results is already holding the mutex and never releases it. This causes run_fake_scan to wait indefinitely for the mutex to be released.

To fix this issue, you'll need to make sure that the mutexQueue mutex is properly released by read_results after it's done accessing the shared data. You can do this by calling pthread_mutex_unlock(&mutexQueue); after read_results is done using the shared data.

Additionally, it looks like you're trying to dynamically modify the thread_submit variable at runtime, but this isn't possible because thread_submit is defined as a preprocessor macro. Preprocessor macros are expanded at compile time, so you can't change their values at runtime. You'll need to use a regular variable if you want to be able to change its value at runtime.

I hope this helps! Let me know if you have any other questions.

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.