thread-safe variables comaprison C VS2010 [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
threaded programming
I want to write simple multi-thread app.
where each thread when it open I increment (using InterlockedIncrement) member by one ,
and decrements it (using InterlockedDecrement) when thread finishes
I know about Mutex/Semaphore/event
but I would more clean /simple way to implement comparison similar to the Interlocked function .
What I need next is implement comparison function [if(member == x)]
simple example:
Thread 1 function:
{
//do somthing
InterlockedDecrement(member);
}
Thread 2 function:
{
//do something else
InterlockedDecrement(member);
}
main thread function :
{
while(member)//<--how can it be done in thread safe fashion
{
//do yet another something
}
}

Use InterlockedAdd and add 0. This will lock the member and return the value without changing it:
while (InterlockedAdd(&member, 0) == someValue)
{
//do yet another something
}

Related

How should I arrange my if...else if in C? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 months ago.
Improve this question
My school teacher teaches if...else if today. She teaches us to write the if...else if like this
if(x == 1) {
//some code here
}
else if(x == 2) {
//some code here
}
else if(x == 3) {
//some code here
}
else if (x == 4) {
//some code here
}
But, from what I learn from Internet is mostly write like this
if(x == 1) {
//some code here
}
else if(x == 2) {
//some code here
}
else if(x == 3) {
//some code here
}
else if (x == 4) {
//some code here
}
So, my question is which should I follow? Internet or my teacher ? I never seen anybody write if...else if like my teacher before.
p.s. I know these two will give same result when running it. Just confusing how should I arrange the if...else if statement.
I normally don't like style debates, but your teacher chose a really strange style indeed.
You are correct, almost everybody writes else if like in your second example, even when the local standard calls for never using single statement blocks with curly braces omitted, as though else if were a keyword with a space in it.
I have seen people ban it, but everybody else who does so always ends up with
else {
if () {
}
}
or a variant of it where one or more opening braces go on their own lines.
We should remember that else if ladders get very long indeed and a style that requires indenting them becomes intolerable after while. Most languages that have more rigid whitespace rules (I'm looking at you Visual Basic) end up with an elseif keyword to avoid this problem.

Using Regular Expression to extract word in a string in C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to extract a small substring from a string of words in c. I did it in a previous program written in python using this code:
out_block = "".join(re.findall(r'.FT off(.*?).FT on',finaltext,re.DOTALL))
The code extracts all the characters in between .FT off and .FT onand passes it to out_block as a string.
I wanted to know how to do the same using regular expressions but in C
How would I convert this code to a C code that does the exact same thing?
Basicly you do this:
#include <sys/types.h>
#include <regex.h>
regex_t finder;
if (0 != regcomp(&finder, "regex-string", 0)) { error handling }
You might need to adjust your regex string above.
After that you can do any number of times:
regmatch_t match[N_MATCH];
if (0 == regexec(&finder, "haystack", N_MATCH, match, 0)) {
process matches
} else {
no match
}
Clean up using regfree and get error messages (in regcomp) using regerror.
For more details, consult man page regex(3).

How to modify the function to satisfy OCP? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The following function is implemented in C:
function(struct_XX *p)
{
if(p->A)
{
if(p->B)
{
do something0;
}
if(p->C)
{
do something1;
}
if(p->D && p->E)
{
do something2;
}
if(p->Z)
{
do something3;
}
}
}
each branch has different things to do,It doesn't satisfy Open-Closed Principle(because the struct which p points at is not stable ,new fields will be added into it often,that means new process codes will be added into function frequently); how can it be modified to satisfy OCP?
you haven't given much to go, you have a bunch of conditions, and a bunch of "do"s based on those conditions.
So given that... you can make something that stores a collections of conditions and callbacks....
void ocpfunction(struct struct_XX *p)
{
int i;
for(i=0; i<p->conditions_count; i++)
{
if(p->conditions[i].evaluate(p))
{
p->conditions[i].callback(p, p->conditions[i].context);
}
}
}

How to get the following output in C programming [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
A logical question asked in the aptitude test.
if(______)
{
printf("Hi");
}
else
{
printf("Hello");
}
What condition should be provided in place of _____ to get the following output ?
HelloHello
You do not need to give control to else twice. All you need is a false condition with a side effect of printing the other "Hello", for example
if (printf("Hello") == 0) // this condition is false, because printf returns the number of chars written
{
printf("Hi");
}
else
{
printf("Hello");
}

C code explanation needed [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Can someone explain the following code?
what is the use of the variable ort?
void squart_root(double a)
{
if (a>0.0){
double root = 1, ort = 0;
while(ort!=root)
{
ort = root;
root = ((a/root) + root) / 2;
}
printf("Root is : %lf",root);
}else if(a==0.00000){
printf("Root is : %lf",a);
}else{
printf("Cannot find the square root of a negative number");
}
}
This looks similar to Newton's method of calculating square root (derived from Newton-Raphson's method).
You can read more about this here.
It looks like:
X(n+1) = (A/X(n) + X(n))/2
It converges when X(n) = X(n+1) (that is in your case) or under some precision.
At each iteration, X(n) should get closer to the real root.
The purpose of ort is to hold X(n) and check for this convergence. When root converges, loop terminates. That is why ort is here.
You can read about Newton-Raphsone method (just google it) and you will be able to derive this equation.

Resources